package com.jiuyin.dianjing.ui.fragment.game;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.DefaultItemAnimator;

import com.bumptech.glide.Glide;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.BaseRecyclerAdapter;
import com.jiuyin.dianjing.adapter.SmartViewHolder;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.decoration.CustomDividerItemDecoration;
import com.jiuyin.dianjing.model.GameInfoItemModel;
import com.jiuyin.dianjing.ui.activity.gameInfo.GameInfoDetailActivity;
import com.jiuyin.dianjing.ui.fragment.base.BaseRefreshLazyFragment;

import org.json.JSONObject;

import java.util.HashMap;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_GAMEID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_HEAD_URL;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_INFORMATIONID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_INTRO;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_NUM;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_SYSTEM_TYPE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TITLE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_URL;
import static com.jiuyin.dianjing.api.constant.ApiConstant.OSS_IMG_SCREEN_TOPIC;
import static com.jiuyin.dianjing.api.constant.ApiConstant.PAGE_NUM;
import static com.jiuyin.dianjing.api.constant.ApiConstant.VALUE_ANDROID;

/**
 * 游戏资讯
 */
public class FragmentGame extends BaseRefreshLazyFragment<GameInfoItemModel> {
    private String mGameId;
    private String mType;

    public FragmentGame() {
        super(PAGE_NUM);
    }

    public static FragmentGame newInstance(String gameId, String type) {
        FragmentGame fragmentGame = new FragmentGame();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_GAMEID, gameId);
        bundle.putString(KEY_TYPE, type);
        fragmentGame.setArguments(bundle);
        return fragmentGame;
    }

    /**
     * 进入获取资讯列表
     */
    @Override
    public void fetchData() {
        HashMap<String, Object> params = new HashMap<>();
        params.put(KEY_TOKEN, HelperApplication.getToken());
        params.put(KEY_NUM, mPageNum);
        params.put(KEY_TYPE, mType);
        params.put(KEY_GAMEID, mGameId);
        params.put(KEY_SYSTEM_TYPE, VALUE_ANDROID);
        JSONObject jsonObject = new JSONObject(params);
        getApiHelper().appAllInformation(jsonObject.toString(), data -> {
            dealWithSuccessInfo(data);
        });
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_game;
    }

    @Override
    protected void initData() {
        Bundle argumentsBundle = getArguments();
        if (null != argumentsBundle) {
            mGameId = argumentsBundle.getString(KEY_GAMEID);
            mType = argumentsBundle.getString(KEY_TYPE);
        }
        mAdapter = new BaseRecyclerAdapter<GameInfoItemModel>(R.layout.item_info_game) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, GameInfoItemModel model, int position) {
                if (null != model) {
                    StringBuilder url = new StringBuilder(model.getShareUrl());
                    url.append(OSS_IMG_SCREEN_TOPIC);
                    LinearLayout typeIf = holder.itemView.findViewById(R.id.ll_type_if);
                    LinearLayout typeAd = holder.itemView.findViewById(R.id.ll_type_ad);
                    if (model.getType().equals("1")) {
                        typeAd.setVisibility(View.VISIBLE);
                        typeIf.setVisibility(View.GONE);
                        holder.text(R.id.tv_ad_title, model.getTitle());
                        ImageView imageView = holder.itemView.findViewById(R.id.img_ad_game);
                        Glide.with(getActivity()).load(url.toString()).into(imageView);
                    } else {
                        typeIf.setVisibility(View.VISIBLE);
                        typeAd.setVisibility(View.GONE);
                        holder.text(R.id.tv_game_info_title, model.getTitle());
                        holder.text(R.id.tv_game_info_comment, model.getComment());
                        holder.text(R.id.tv_game_info_check, model.getCheck());
                        holder.text(R.id.tv_game_info_time, model.getTime());
                        ImageView imageView = holder.itemView.findViewById(R.id.img_game);
                        Glide.with(getActivity()).load(url.toString()).into(imageView);
                    }
                }
            }
        };
        mAdapter.setOnItemClickListener((parent, view, position, id) -> {
            GameInfoItemModel model = mAdapter.getItem(position);
            if (model.getType().equals("0")) {
                Bundle bundle = new Bundle();
                bundle.putString(KEY_URL, model.getInfoUrl());
                bundle.putString(KEY_HEAD_URL, model.getShareUrl());
                bundle.putString(KEY_INFORMATIONID, model.getInformationId());
                bundle.putString(KEY_TITLE, model.getTitle());
                bundle.putString(KEY_INTRO, model.getIntro());
                goTargetActivity(GameInfoDetailActivity.class, bundle);
            } else {
                gotoBrowser(model.getAdvertising_link());
            }
        });

        mRcvPage.addItemDecoration(new CustomDividerItemDecoration(getContext()));//添加分割线
        mRcvPage.setItemAnimator(new DefaultItemAnimator());
        mRcvPage.setAdapter(mAdapter);
    }

    @Override
    protected void initView() {
    }

    /**
     * 跳转浏览器
     */
    private void gotoBrowser(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri content_url = Uri.parse(url);
        intent.setData(content_url);
        startActivity(intent);
    }
}